package com.asset.admin.biz.impl;

import com.asset.admin.biz.ILoginBiz;
import com.asset.common.config.Conts;
import com.asset.common.enums.ResultCodeEnum;
import com.asset.common.result.ResultMsg;
import com.asset.common.utils.CookieUtil;
import com.asset.common.utils.JsonUtil;
import com.asset.common.utils.ThreadLocalUtil;
import com.asset.common.utils.encrypt.EncryptUtil;
import com.asset.user.dao.entity.UserEntity;
import com.asset.user.form.UserForm;
import com.asset.user.sevice.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 * 登录相关实现
 * </p>
 *
 * @author 牧之
 * @date 2024/2/20 15:26
 */
@Service
@Slf4j
public class LoginBizImpl implements ILoginBiz {

    @Resource
    private IUserService userService;

    @Override
    public ResultMsg<Object> login(UserForm userForm, HttpServletResponse response) {
        ResultMsg<Object> resultMsg = new ResultMsg<>();
        if (userForm == null || userForm.getTelephone() == null || userForm.getPassword() == null) {
            resultMsg.setCode(ResultCodeEnum.FAIL.getCode());
            resultMsg.setMsg("参数异常");
            return resultMsg;
        }
//        if (!userForm.getTelephone().matches("^1[0-9]{10}$")) {
//            resultMsg.setCode(ResultCodeEnum.FAIL.getCode());
//            resultMsg.setMsg("请使用正确的手机号进行登录");
//            return resultMsg;
//        }
        UserEntity resultUserEntity = userService.getUserByTel(userForm.getTelephone());
        if (resultUserEntity == null) {
            resultMsg.setCode(ResultCodeEnum.FAIL.getCode());
            resultMsg.setMsg("该手机号未注册，请注册后登录");
            return resultMsg;
        }
        if (!resultUserEntity.getPassword().equals(EncryptUtil.encrypt(userForm.getPassword(),UserEntity.class))) {
            resultMsg.setCode(ResultCodeEnum.FAIL.getCode());
            resultMsg.setMsg("密码错误，请重试");
            return resultMsg;
        }
        resultMsg.setCode(ResultCodeEnum.SUCCESS.getCode());
        resultMsg.setMsg("登录成功");

        //登录成功，设置cookie
        if (resultMsg.getCode().equals(ResultCodeEnum.SUCCESS.getCode())) {
            Cookie cookie = CookieUtil.buildCookie(Conts.CURRENT_USER_ID, EncryptUtil.encrypt(String.valueOf(resultUserEntity.getId()),"resultUserEntity"), 24 * 60 * 60, String.valueOf(ThreadLocalUtil.getInstance().get(Conts.ROOT_DOMAIN)));
            log.info("cookie = " + JsonUtil.toJson(cookie));
            response.addCookie(cookie);
        }

        return resultMsg;
    }

    @Override
    public ResultMsg<Object> register(UserForm userForm) {
        ResultMsg<Object> resultMsg = new ResultMsg<>();
        if (userForm == null || userForm.getGender() == null || userForm.getName() == null || userForm.getTelephone() == null || userForm.getPassword() == null) {
            resultMsg.setCode(ResultCodeEnum.FAIL.getCode());
            resultMsg.setMsg("参数异常");
            return resultMsg;
        }
        if (!userForm.getTelephone().matches("^1[0-9]{10}$")) {
            resultMsg.setCode(ResultCodeEnum.FAIL.getCode());
            resultMsg.setMsg("请使用正确的手机号进行注册");
            return resultMsg;
        }

        UserEntity resultUserEntity = userService.getUserByTel(userForm.getTelephone());
        if (resultUserEntity != null) {
            resultMsg.setCode(ResultCodeEnum.FAIL.getCode());
            resultMsg.setMsg("手机号已被注册");
            return resultMsg;
        }
        try {
            UserEntity entity = UserForm.convert2Entity(userForm);
            entity.setPassword(EncryptUtil.encrypt(entity.getPassword(),UserEntity.class));
            userService.register(entity);
        } catch (Exception e) {
            resultMsg.setCode(ResultCodeEnum.FAIL.getCode());
            resultMsg.setMsg("注册失败！请重试");
            return resultMsg;
        }
        resultMsg.setCode(ResultCodeEnum.SUCCESS.getCode());
        resultMsg.setMsg("注册成功，请登录");
        return resultMsg;
    }
}
